package cn.dnui_zjq823.dnuifood_16110100823.adapter;


import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.bean.CollectionBean;
import cn.dnui_zjq823.dnuifood_16110100823.bean.ShopBean;
import cn.dnui_zjq823.dnuifood_16110100823.detail.ShopDetail;

/**
 * Created by dell on 2018/10/29.
 */

public class CShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private List<CollectionBean> list;
    private Context context;
    private String url="http://172.24.10.175:8080/foodService/";

    public CShopAdapter(Context context) {
        this.context = context;
    }

    public void setList(List<CollectionBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.shop_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final CollectionBean collectionBean=list.get(position);
        if (collectionBean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        Picasso.with(context).load(url+collectionBean.getPic()).fit().placeholder(R.mipmap.ic_launcher).into(viewHolder.shop_pic);
        viewHolder.tv_shopname.setText(collectionBean.getShopname());
        viewHolder.tv_shoplevel.setText("");
        viewHolder.tv_shopaddress.setText(collectionBean.getAddress());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str=String.valueOf(list.get(position).getShop_id());
                String str2=String.valueOf(list.get(position).getShopname());
                String str3=String.valueOf(list.get(position).getAddress());
                String str6=String.valueOf(list.get(position).getPic());
                Toast.makeText(context,"id"+str, Toast.LENGTH_SHORT).show();
                Intent intent= new Intent(context, ShopDetail.class);
                intent.putExtra("shopid",str);
                intent.putExtra("shopname",str2);
                intent.putExtra("shopaddress",str3);
                intent.putExtra("shoppic",str6);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
            return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_shopname,tv_shoplevel,tv_shopaddress;
        ImageView shop_pic;
        public ViewHolder(View itemView) {
            super(itemView);
            shop_pic=(ImageView) itemView.findViewById(R.id.shop_pic);
            tv_shopname=(TextView)itemView.findViewById(R.id.tv_shopname);
            tv_shoplevel=(TextView)itemView.findViewById(R.id.tv_shoplevel);
            tv_shopaddress=(TextView)itemView.findViewById(R.id.tv_shopaddress);

        }
    }
}
